﻿package com.saint.video {
	
	/**
	 * main display class for simple video player
	 * with playback and controls
	 *
	 * @author wattonn
	 */
	

	import com.saint.utils.ImageLoader;
	import com.saint.video.events.VideoEvent;
	import com.saint.video.VideoController;
	import com.saint.video.videoControls.VideoControls;
	import flash.display.Stage;
	
	import com.saint.utils.Logger;
	import com.saint.video.SimpleVideo;
	import com.saint.video.videoControls.VideoControls;
	import com.saint.video.data.VideoDataObject;
	
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	public class VideoPlayerApplication extends Sprite {
		
		/* THESE SETTINGS ARE SET IN THE VideoPlayerConfig class 
		 * 
		 * DO NOT EDIT HERE
		 */
		
		 
		 
		 
		 
		 

		/*--------------------------------------------------------------------------*/
		
		
							/* NO EDITY VARIABLES HERE  */
		
		
		/*--------------------------------------------------------------------------*/
		 
		
		
		 
		
		/*	autoplay
		 *
		 * whether the video should play aUtomatically once loaded */
		private var autoPlay:Boolean;
		
		/*	showControls
		 *
		 * set to true to show controls, false to have no video controls */
		private var showControls:Boolean;
		
		/* holdingImage
		 *
		 * file path to an image loaded in behind the video player - will be viewable if autoplay is set to false */
		private var holdingImage:String;
		
		 /*standAlonePlayer
		  *
		  * Boolean to indicating if the video player is the only object in the swf,
		  * and as a resule will fill all the available area*/
		  private var standAlonePlayer:Boolean;  
		

		 /*set player size
		   *
		   * variables representing the width and height of the video player screen */
		  private var videoScreenWidth:Number; 
		  private var videoScreenHeight:Number;
		  
		  
		/*stretchToFit
		   * 
		   * decides if the player will stretch to fit the available area or keep its ration in the available areaa*/
		  private var stretchToFit:Boolean;


		/* autoSize
		 * 
		 * 	whether the video automaticall fills as muxh area or not
		 *  true:scale to fit available are
		 *  false: it will stay in the origional flv size settings
		 */
		private var reSizeFlv:Boolean; 
	
		
		
		/* ------------------------------------------------------------------------------------------------------------------------------*/
		
		
		
		
		private var holdingImageHolder:ImageLoader;
		private var playerContainer:Sprite;
		private var videoContainer:Sprite;
		private var controlsContainer:Sprite;
		private var videoPlayer:SimpleVideo;
		private var videoControls:VideoControls;
		private var videoController:VideoController;
		private var videoFile:String;
		
		private var controlsHeight:uint = 18;
		private var controlsY:uint;
		private var playerSize:uint;
		private var mainStage:Stage
		
				
		public function VideoPlayerApplication(mainStage:Stage,playerConfig:VideoPlayerConfig = null):void 
		{			
			Logger.info("\nVideoPlayerApplication.VideoPlayerApplication mainStage: " + mainStage);		
			this.mainStage = mainStage;
			setPlayerVariables(playerConfig);		
			buildPlayer();						
		}
		
		
		private function setPlayerVariables(playerConfig:VideoPlayerConfig):void
		{
			Logger.info("VideoPlayerApplicattion.setPlayerSettings : " + playerConfig);
			
			var videoPlayerSettings:VideoPlayerConfig = playerConfig;
			(playerConfig == null) ? videoPlayerSettings = new VideoPlayerConfig() : videoPlayerSettings = playerConfig;
			
			autoPlay = videoPlayerSettings.autoPlay;
			holdingImage = videoPlayerSettings.holdingImage;
			reSizeFlv = videoPlayerSettings.reSizeFlv;
			showControls = videoPlayerSettings.showControls;
			standAlonePlayer = videoPlayerSettings.standAlonePlayer;
			stretchToFit = videoPlayerSettings.stretchToFit;
			videoScreenHeight = videoPlayerSettings.videoScreenHeight;
			videoScreenWidth = videoPlayerSettings.videoScreenWidth;
			videoFile = videoPlayerSettings.videoFile;
			
			
		}
		
		private function buildPlayer():void {
						
			playerContainer = new Sprite();
			
			var videoData:VideoDataObject = new VideoDataObject();
			
			// if standAlone set the video screen to fill the whole stage and set controls to overlay
			if (standAlonePlayer)
			{
				videoData.videoAreaWidth = mainStage.stageWidth;
				videoData.videoAreaHeight = mainStage.stageHeight ;
			}
			else
			{
				videoData.videoAreaWidth = videoScreenWidth;
				videoData.videoAreaHeight = videoScreenHeight;				
			}
			
			
			/* create the video screen */
			videoPlayer = new SimpleVideo();
			videoPlayer.initVideo(videoData);
			//videoPlayer.addEventListener(VideoEvent.PLAY_STOPPED, endOfStreamHandler, false, 0, true);
			
			/* create the video controls */
			videoControls = new VideoControls(videoPlayer, mainStage.stageWidth, controlsHeight, autoPlay);
			videoControls.y = mainStage.stageHeight - controlsHeight ;		
			
			videoContainer = new Sprite();
			videoContainer.addChild(videoPlayer);
			playerContainer.addChild(videoContainer);
			
			/* create the controller  - passing screen and controls */
			if (showControls) 
			{	
				videoController = new VideoController(videoPlayer, videoControls, playerContainer);
				videoController.videoToPlay = videoFile;
				videoController.init(autoPlay);
				controlsContainer = new Sprite();
				controlsContainer.addChild(videoControls);
				playerContainer.addChild(controlsContainer);
			}
			
			checkForHoldingImage();
			
			addChild(playerContainer);
			
			//TODO - remove this border - for testing
			var borderHolder:Sprite = new Sprite()
			borderHolder.graphics.lineStyle(1, 0xffcc00, 1);
			borderHolder.graphics.drawRect(0, 0, videoScreenWidth, videoScreenHeight);
			addChild(borderHolder);
		}
		
		
		
		private function checkForHoldingImage():void
		{
			if (holdingImage	!= null)
			{
				holdingImageHolder = new ImageLoader(holdingImage);
				addChild(holdingImageHolder);
			}
		}
		
			
		private function endOfStreamHandler(event:VideoEvent):void {
			Logger.info("end of stream\n        - trigger GUI event if required")
		}
		
		//TODO
		/*
		 *reset the videoplayer and start playing a new video
		 * 
		 */
		public function playNewVideo(flvName:String)
		{
			Logger.info("vidoePlayerApplication.playNewVideo");
			videoController.playNewVideo(flvName);
		}
	}
}